$(function () {

    var salary_datagrid =  $('#salary_datagrid');
    var attYearMonth = $('#attYearMonth');
    //数据表格
    salary_datagrid.datagrid({
        url:'/salary/list.do',
        striped:true,
        singleSelect:true,
        pagination:true,
        rownumbers:true,
        loadMsg:"别慌,正在努力加载",
        toolbar:"#tb",
        columns:[[
            {field:'cb',checkbox:true},
            {field:'id',hidden:true,width:100},
            {field:'name',title:'姓名',width:100},
            {field:'dept',title:'部门',width:100,align:'right',formatter:deptFormatter},
            {field:'month',title:'月份',width:100,align:'right'},
            {field:'baseSalary',title:'基本工资',width:100,align:'right'},
            {field:'accumulationFund',title:'公积金',width:100,align:'right'},
            {field:'socialInsurance',title:'五险',width:100,align:'right'},
            {field:'overtime',title:'加班补贴',width:100,align:'right'},
            {field:'personalIncome',title:'个人所得税',width:100,align:'right'},
            {field:'workday',title:'工作天数',width:100,align:'right'},
            {field:'bankcardNumber',title:'工资卡号',width:100,align:'right'},
            {field:'lateday',title:'迟到天数',width:100,align:'right'},
            {field:'realWages',title:'实发工资',width:100,align:'right'},
            {field:'bonus',title:'奖金',width:100,align:'right'},
            {field:'coefficient',title:'奖金系数',width:100,align:'right'}
        ]]
       /* //首次不加载数据
        onBeforeLoad: function (param) {
            var firstLoad = $(this).attr("firstLoad");
            if (firstLoad == "false" || typeof (firstLoad) == "undefined")
            {
                $(this).attr("firstLoad","true");
                return false;
            }
            return true;
        }*/
    });
    //end
    var cmdObj = {
        //save按钮提交表单
        lead : function () {
            $.messager.alert("温馨提示","导入个毛线,没这个功能");
        },
        //刷新
        reload :function () {
            salary_datagrid.datagrid("reload");
        },
        //高级查询
        //加载职工列表时,将多余查询条件一起发起请求
        searchT:function(){
            //加载列表数据时,携带额外参数
            salary_datagrid.datagrid("load",{
                monthId:attYearMonth.val()
            })
        }
    };
    //抽取优化
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });
    //end

    //年月格式化
    attYearMonth.datebox({
        //显示日趋选择对象后再触发弹出月份层的事件，初始化时没有生成月份层
        onShowPanel: function () {
            //触发click事件弹出月份层
            span.trigger('click');
            if (!tds)
            //延时触发获取月份对象，因为上面的事件触发和对象生成有时间间隔
                setTimeout(function() {
                    tds = p.find('div.calendar-menu-month-inner td');
                    tds.click(function(e) {
                        //禁止冒泡执行easyui给月份绑定的事件
                        e.stopPropagation();
                        //得到年份
                        var year = /\d{4}/.exec(span.html())[0] ,
                            //月份
                            //之前是这样的month = parseInt($(this).attr('abbr'), 10) + 1;
                            month = parseInt($(this).attr('abbr'), 10);

                        //隐藏日期对象
                        $('#attYearMonth').datebox('hidePanel')
                        //设置日期的值
                            .datebox('setValue', year + '-' + month);
                    });
                }, 0);
        },
        //配置parser，返回选择的日期
        parser: function (s) {
            if (!s) return new Date();
            var arr = s.split('-');
            return new Date(parseInt(arr[0], 10), parseInt(arr[1], 10) - 1, 1);
        },
        //配置formatter，只返回年月 之前是这样的d.getFullYear() + '-' +(d.getMonth());
        formatter: function (d) {
            var currentMonth = (d.getMonth()+1);
            var currentMonthStr = currentMonth < 10 ? ('0' + currentMonth) : (currentMonth + '');
            return d.getFullYear() + '-' + currentMonthStr;
        }
    });
    //日期选择对象
    var p = attYearMonth.datebox('panel'),
        //日期选择对象中月份
        tds = false,
        //显示月份层的触发控件
        span = p.find('span.calendar-text');
    var curr_time = new Date();

    //设置前当月
    attYearMonth.datebox("setValue", myformatter(curr_time));
    //end
});

//部门格式化
function deptFormatter(value,row,index) {
    return value ? value.name: "";
}
//职工名字格式化
function staffjobFormatter(value,row,index) {

    return value ? value.username: "";
}

//格式化日期
function myformatter(date) {
    //获取年份
    var y = date.getFullYear();
    //获取月份
    var m = date.getMonth() + 1;
    return y + '-' + m;
}



